/*
 * @Author: 0x9DEFA478
 * @Date: 2022-01-23 15:31:56
 * @LastEditTime: 2022-03-06 20:27:17
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "./H_NES_Console.h"




static void H_NES_Console_Process(void (*RendrsFrame)(H_NES_Console*),H_NES_Console* Console){
  H_NES_Message msg;
  int (*MessageTryReceive)(void*,void*);
  void* Command;

  MessageTryReceive=Console->LL_Function.Thread.MessageTryReceive;
  Command=Console->MessageQueue.Command;

  for(;;){
    
    //读取控制器状态
    Console->StandardController.SrcStatus=Console->LL_Function.Console.GetStandardControllerStatus(Console->LL_Function.v);
    RendrsFrame(Console);
    //读取控制器状态
    Console->StandardController.SrcStatus=Console->LL_Function.Console.GetStandardControllerStatus(Console->LL_Function.v);
    RendrsFrame(Console);

    while(0==MessageTryReceive(Command,&msg)){
      Hbyte* p;
      Hint32* p_int32;

      //有事务处理
      switch(msg.FunctionIndex){
        case vH_NES_Message_FunctionIndex_GetProgress:
          p=msg.v;
          H_NES_Console_GetProgress(Console,p);
          p=&p[vH_NES_Console_ProgressSize];
          Console->Cartridge.Progress.Get(Console,p);
          Console->LL_Function.Thread.MessageSned(Console->MessageQueue.Ack,&msg);
          break;
        case vH_NES_Message_FunctionIndex_LoadProgress:
          p=msg.v;
          H_NES_Console_LoadProgress(Console,p);
          p=&p[vH_NES_Console_ProgressSize];
          Console->Cartridge.Progress.Load(Console,p);
          Console->LL_Function.Thread.MessageSned(Console->MessageQueue.Ack,&msg);
          break;
        case vH_NES_Message_FunctionIndex_Stop:
          goto Return;
        case vH_NES_Message_FunctionIndex_SetVblankNMI_Delay:
          p_int32=msg.v;
          Console->CompatibilityOption.VblankNMI_Delay=p_int32[0];
          Console->LL_Function.Thread.MessageSned(Console->MessageQueue.Ack,&msg);
          break;
        case vH_NES_Message_FunctionIndex_SetMMC4_CountDelay:
          p_int32=msg.v;
          Console->CompatibilityOption.MMC4_CountDelay=p_int32[0];
          Console->LL_Function.Thread.MessageSned(Console->MessageQueue.Ack,&msg);
          break;
        default:
          for(;;){
            //不可能的情况
          }
      }
    }
  }

  void (*ThreadExit)(int);
Return:
  ThreadExit=Console->LL_Function.Thread.ThreadExit;
  Console->RetVal=0;
  H_NES_Console_PreExit(Console);
  oH_NES_Printf("\r\nH_NES:线程正常退出\r\n");
  ThreadExit(0);
}




void H_NES_Console_Start(H_NES_Console* Console){

  Console->StandardController.SrcStatus=0U;
  Console->StandardController.Controller1=0U;
  Console->StandardController.Controller2=0U;
  Console->StandardController.Reg_4016_W=0U;

  H_NES_PPU_Reset(Console);
  H_NES_APU_Reset(Console);
  H_NES_CPU_Reset(Console);
  switch(Console->TelevisionStandards){
    case vH_NES_TelevisionStandards_NTSC:
      H_NES_Console_Process(H_NES_PPU_RendrsFrame_NTSC,Console);
      break;
    case vH_NES_TelevisionStandards_PAL:
      H_NES_Console_Process(H_NES_PPU_RendrsFrame_PAL,Console);
      break;
    case vH_NES_TelevisionStandards_Dendy:
      H_NES_Console_Process(H_NES_PPU_RendrsFrame_Dendy,Console);
      break;
    default:
      H_NES_Console_Process(H_NES_PPU_RendrsFrame_NTSC,Console);
      break;
  }

  for(;;){
    //正常情况 不会进入此循环
  }
}



